package br.ufrn.imd.classificados.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import br.ufrn.imd.classificados.tasks.ApiPostTask;
import br.ufrn.imd.classificados.tasks.ApiPutTask;
import br.ufrn.imd.imd0023_classificados.R;

import com.actionbarsherlock.app.SherlockActivity;

public class EditarPerfilActivity extends SherlockActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.editar_perfil);

		SharedPreferences settings = getSharedPreferences("UserProfile", MODE_PRIVATE);
		((EditText) findViewById(R.id.emailUsuario)).setText(settings.getString("email", ""));
		((EditText) findViewById(R.id.nomeUsuario)).setText(settings.getString("name", ""));
		((EditText) findViewById(R.id.telefoneUsuario)).setText(settings.getString("phone", ""));

		Button salvarPerfilButton = (Button) findViewById(R.id.salvarPerfil);
		salvarPerfilButton.setOnClickListener(new OnClickListener() {
	
			@Override
			public void onClick(View v) {
				SharedPreferences settings = getSharedPreferences("UserProfile", MODE_PRIVATE);
				SharedPreferences.Editor editor = settings.edit();
				String user_email = settings.getString("email", "");
				
				EditText emailEditText = (EditText) findViewById(R.id.emailUsuario);
				EditText nameEditText = (EditText) findViewById(R.id.nomeUsuario);
				EditText phoneEditText = (EditText) findViewById(R.id.telefoneUsuario);
				
				if (user_email == "") {
					sendPost(
						emailEditText.getText().toString(),
						nameEditText.getText().toString(),
						phoneEditText.getText().toString()
					);
				} else {
					sendPut(
						user_email,
						emailEditText.getText().toString(),
						nameEditText.getText().toString(),
						phoneEditText.getText().toString()
					);
				}

				editor.putString("email", emailEditText.getText().toString().trim());
				editor.putString("name", nameEditText.getText().toString().trim());
				editor.putString("phone", phoneEditText.getText().toString().trim());
				editor.apply();

				Toast.makeText(getBaseContext(), "Perfil foi salvo", Toast.LENGTH_LONG).show();
				finish();
			}
		});
	}

	private void sendPost(String email, String name, String phone) {
	    JSONObject jsonObj = new JSONObject();

	    try {
	    	jsonObj.put("email", email);
	    	jsonObj.put("name", name);
	    	jsonObj.put("phone", phone);
	    } catch (JSONException e) {
	    	e.printStackTrace();
	    	Log.i("JSON Exception", e + "");
	    }

	    new ApiPostTask("users").execute(jsonObj);
	}

	private void sendPut(String user_email, String email, String name, String phone) {
	    JSONObject jsonObj = new JSONObject();

	    try {
	    	jsonObj.put("user_email", user_email);
	    	jsonObj.put("email", email);
	    	jsonObj.put("name", name);
	    	jsonObj.put("phone", phone);
	    } catch (JSONException e) {
	    	e.printStackTrace();
	    	Log.i("JSON Exception", e + "");
	    }

	    new ApiPutTask("users").execute(jsonObj);
	}
}
